import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Label;


public class changepassword extends Dialog {

	protected Object result;
	protected Shell shell;
  protected ClientLogic logic;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public changepassword(Shell parent, int style) {
		super(parent, style);
		setText("Change Password");
        logic = ClientLogic.getInstance();
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 300);
		shell.setText(getText());
		
		org.eclipse.swt.widgets.Label lblNewPassword = new org.eclipse.swt.widgets.Label(shell, SWT.NONE);
		lblNewPassword.setBounds(10, 81, 135, 17);
		lblNewPassword.setText("New Password:");
		
		final Text Password = new Text(shell, SWT.BORDER | SWT.PASSWORD);
		Password.setBounds(151, 71, 287, 27);
		
		org.eclipse.swt.widgets.Label lblRetype = new org.eclipse.swt.widgets.Label(shell, SWT.NONE);
		lblRetype.setText("Retype Password:");
		lblRetype.setBounds(10, 114, 135, 17);
		
		final Text RetypePassword = new Text(shell, SWT.BORDER | SWT.PASSWORD);
		RetypePassword.setBounds(151, 104, 287, 27);
		
		final Label resultLabel = new Label(shell, SWT.NONE);
    resultLabel.setBounds(151, 137, 287, 17);
		resultLabel.setVisible(false);
		
		Button btnConfirm = new Button(shell, SWT.NONE);
		btnConfirm.setBounds(84, 203, 91, 29);
		btnConfirm.setText("Confirm");
		btnConfirm.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				if (Password.getText().equals("")||RetypePassword.getText().equals("")) {
					resultLabel.setText("No field can be blank.");
				} else if (!Password.getText().equals(RetypePassword.getText())) {
					resultLabel.setText("The password fields don't match.");
					Password.setText("");
					RetypePassword.setText("");
				} else
					try {
						if (logic.changePassword(Password.getText())) {
							resultLabel.setText("You password has been changed.");
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
        resultLabel.setVisible(true);
			}
		});
		
		Button btnCancel = new Button(shell, SWT.NONE);
		btnCancel.setBounds(263, 203, 91, 29);
		btnCancel.setText("Close");
		btnCancel.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				shell.close();
			}
		});
		
	}

}
